/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef TRAYICONWATCHER_H
#define TRAYICONWATCHER_H

#include <QObject>
#include <QGSettings>
#include <QDBusConnection>
#include <QDBusPendingCall>
#include <QDBusPendingReply>
#include <QDebug>

#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
#include <QtQml/qqml.h>
#endif

class TrayIconWatcher : public QObject
{
    Q_OBJECT
#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
    QML_ELEMENT
#endif
    Q_PROPERTY(QString iconSource READ iconSource WRITE setIconSource NOTIFY iconSourceChanged)
    Q_PROPERTY(int type READ type WRITE setType NOTIFY typeChanged)
    Q_PROPERTY(QString timeformat READ timeformat WRITE settimeformat NOTIFY timeformatChanged)
    Q_PROPERTY(QString dateformat READ dateformat WRITE setdateformat NOTIFY dateformatChanged)

public:
    enum IconType {
        Volume,
        Power,
        Notification,
        Network,
        Bluetooth,
        TimeDate,
    };
    Q_ENUMS(IconType)

    enum class NetType {
        Wire,
        Wireless,
        Unconnected,
    };

    explicit TrayIconWatcher(QObject *parent = nullptr);

    QString iconSource();
    void setIconSource(const QString &source);

    int type();
    void setType(const int &type);

    QString timeformat();
    void settimeformat(const QString &timeformat);

    QString dateformat();
    void setdateformat(const QString &dateformat);

private:
    void asyncCallDbus(QDBusInterface &interface,
                       const QString &method,
                       const QList<QVariant> &argList,
                       const std::function<void(QDBusPendingCallWatcher* call)>&callBack);
    void getVolume();
    void initPowerDbusAddress();
    void setPowerValue(const QDBusPendingReply<QVariant> percentageReply);
    void setBluetoothState();
    void watchBluetoothService();
    void getTimeDateState();
    void getNetConnectType(const QString objPath);
    void getWireNetState();
    void getWirelessNetState(const QString objPath);
    void getWirelessNetValuePath(const QString objPath);
    void getWirelessNetValue(const QString objPathSpecificObject);

    void setVolumeIconByValue(int value);
    void setPowerIconByValue(int powerStateValue, int powerValue);
    void setNotificationIconByValue(bool value);
    void setNetIconByValue(int value, TrayIconWatcher::NetType netType);
    void setBluetoothIconByValue(bool state);

private:    
    QString m_iconSource;
    int m_type;
    QString m_timeformat;
    QString m_dateformat;
    QGSettings *timeSetting;
    QString powerpath;
    bool wireNetExist = false;

signals:
    void iconSourceChanged();
    void typeChanged();
    void timeformatChanged();
    void dateformatChanged();

public slots:
    void initPowerValue();
    void getWifiValue();
    void getNotificationState();
    void getBluetoothSwitch();
    void setVolume(int sound);
    void setMuteState(bool state);
};

#endif // TRAYICONWATCHER_H
